package com.wtao.udiskreadwrite;

import android.hardware.usb.UsbDevice;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.github.mjdev.libaums.UsbMassStorageDevice;
import com.github.mjdev.libaums.fs.UsbFile;
import com.wtao.udiskreadwrite.utils.USBBroadCastReceiver;
import com.wtao.udiskreadwrite.utils.USBHelper;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class GacDebugActivity extends AppCompatActivity implements USBBroadCastReceiver.UsbListener {
    private String TAG = "GacDebugActivity";
    private TextView logResult;
    private USBHelper usbHelper;
    private boolean hasUsbInsert;
    private ArrayList<UsbFile> usbList;
    private ArrayList<UsbFile> targetUsbFileList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable  Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_debug);
        logResult = findViewById(R.id.logResult);
        usbHelper = new USBHelper(this, this);
        usbList = new ArrayList<>();
        findViewById(R.id.text).setOnClickListener(v->{
            Log.e(TAG, "点击获取日志！");
            doGetLogFromECU();
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        usbHelper.registerReceiver();
    }

    private void initUsbFileList() {
        UsbMassStorageDevice[] usbMassStorageDevices = usbHelper.getDeviceList();
        if (usbMassStorageDevices.length <= 0) {
            usbList.clear();
            Log.e(TAG, "No Usb Device");
            return;
        }
        //存在USB
        usbList.clear();
        usbList.addAll(usbHelper.readDevice(usbMassStorageDevices[0]));
    }

    private void doGetLogFromECU() {
        initUsbFileList();
        File file = new File(getExternalFilesDir("EcuLog").getAbsolutePath());
        if (!file.exists()) {
            file.mkdirs();
        }
        File logFile = new File(file, "ecuLog.tar.gz");
        if(logFile.exists()){
            requestECULog(logFile.getAbsolutePath());
        }else{
            Log.e(TAG, "日志文件不存在");
        }
    }
    private void requestECULog( String logPath) {
            if (usbList == null || usbList.size() <= 0) return;
            UsbFile usbFile = null;
            for (int i = 0; i < usbList.size(); i++) {
                if (GlobalConstant.USB_ECU_LOG_DIR_NAME.equals(usbList.get(i).getName())) {
                    usbFile = usbList.get(i);
                    break;
                }
            }
            if (usbFile == null) {
                try {
                    usbFile = usbHelper.getCurrentFolder().createDirectory(GlobalConstant.USB_ECU_LOG_DIR_NAME);
                } catch (IOException e) {
                    Log.e(TAG, "checkLogFile: " + e.getMessage());
                    e.printStackTrace();
                }
            }
            File logFile = new File(logPath);
            boolean result = usbHelper.saveSDFileToUsb(logFile, usbFile, null);

            logResult.setText(result ? "获取日志成功" : "获取日志失败");

    }
    @Override
    public void insertUsb(UsbDevice device_add) {
        hasUsbInsert = true;
        initUsbFileList();
        Log.e(TAG, "检测到U盘");
    }

    @Override
    public void removeUsb(UsbDevice device_remove) {

    }

    @Override
    public void getReadUsbPermission(UsbDevice usbDevice) {

    }

    @Override
    public void failedReadUsb(UsbDevice usbDevice) {

    }
}
